import {Layout} from '../../src/Layout';
export default Layout;

import docs from 'docs:react-aria-components';
import {Switch as VanillaSwitch} from 'vanilla-starter/Switch';
import {Switch as TailwindSwitch} from 'tailwind-starter/Switch';
import '../../tailwind/tailwind.css';
import Anatomy from '@react-aria/switch/docs/anatomy.svg';

export const tags = ['toggle', 'input'];
export const relatedPages = [{'title': 'useSwitch', 'url': 'Switch/useSwitch.html'}];
export const description = 'Allows a user to turn a setting on or off.';

# Switch

<PageDescription>{docs.exports.Switch.description}</PageDescription>

<ExampleSwitcher>
  <VisualExample
    component={VanillaSwitch}
    docs={docs.exports.Switch}
    links={docs.links}
    props={['children', 'isDisabled']}
    initialProps={{children: 'Low power mode'}}
    type="vanilla"
    files={["starters/docs/src/Switch.tsx", "starters/docs/src/Switch.css"]} />
  <VisualExample
    component={TailwindSwitch}
    docs={docs.exports.Switch}
    links={docs.links}
    props={['children', 'isDisabled']}
    initialProps={{children: 'Low power mode'}}
    type="tailwind"
    files={["starters/tailwind/src/Switch.tsx"]} />
</ExampleSwitcher>

## Selection

Use the `isSelected` or `defaultSelected` prop to set the selection state, and `onChange` to handle selection changes.

```tsx render
"use client";
import {Switch} from 'vanilla-starter/Switch';
import {useState} from 'react';

function Example(props) {
  let [selected, setSelection] = useState(false);

  return (
    <>
      <Switch
        {...props}
        ///- begin highlight -///
        isSelected={selected}
        onChange={setSelection}
        ///- end highlight -///
      >
        Low power mode
      </Switch>
      <p>{selected ? 'Low' : 'High'} power mode active.</p>
    </>
  );
}
```

## Forms

Use the `name` and `value` props to submit the switch to the server. See the [Forms](forms) guide to learn more.

```tsx render
"use client";
import {Switch} from 'vanilla-starter/Switch';
import {Button} from 'vanilla-starter/Button';
import {Form} from 'vanilla-starter/Form';

<Form>
  {/*- begin highlight -*/}
  <Switch name="wifi">Wi-Fi</Switch>
  {/*- end highlight -*/}
  <Button type="submit" style={{marginTop: 8}}>Submit</Button>
</Form>
```

## API

<Anatomy />

```tsx links={{Switch: '#switch'}}
<Switch>Label</Switch>
```

### Switch

<PropTable component={docs.exports.Switch} links={docs.links} />
